package com.swd2010.mobileSearch;

import java.util.ArrayList;

import com.swd2010.mobileSearch.data.PersonBean;
import com.swd2010.mobileSearch.data.internalTransfer.ApplicationDataTransfer;

import android.app.Activity;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentUris;
import android.content.Intent;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import android.provider.Contacts;
import android.provider.Contacts.People;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.telephony.PhoneNumberUtils;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.URLSpan;


public class PersonenDetailActivity extends Activity implements OnClickListener {
	
	
	PersonBean pB = null;
	
	
	TextView  nameTV            =   null;	
	TextView  addInformationTV  =   null;
	TextView  telephoneTV       =   null;  
	TextView  eMailTV           =   null;
	TextView  dienstOrtTV       =   null;
	Button    importContact     =   null;
	

	
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.personen_detail_layout);
        
        //To let the textview look like a link
        SpannableStringBuilder ssb = new SpannableStringBuilder();
        
        pB = ApplicationDataTransfer.getInstance().getCurrentWorkingPerson();
        
        nameTV =  (TextView)  findViewById(R.id.nameTextViewId);
        addInformationTV = (TextView)  findViewById(R.id.additionalInformation2TextViewId);
        telephoneTV =  (TextView)  findViewById(R.id.telephone2TextViewId);        
    	eMailTV =  (TextView)  findViewById(R.id.eMail2TextViewId);
    	importContact = (Button) findViewById(R.id.buttonKontaktlisteUeberNehmenId);
    	
    	//telephoneTV.setTextColor(Color.WHITE);
    	//eMailTV.setTextColor(Color.WHITE);
    	
    	
    	importContact.setOnClickListener(this);
    	
    	
    	//dienstOrtTV.setAutoLinkMask(Linkify.ALL);
    	dienstOrtTV =  (TextView)  findViewById(R.id.adresse2TextViewId);        

    	//dienstOrtTV.setTextColor(Color.WHITE);
        
    	
    	nameTV.setText( pB.getFullNameWithDegress() );
    	addInformationTV.setText( pB.getAdditionalInformation() );
        telephoneTV.setText( PhoneNumberUtils.formatNumber( pB.getTelephoneNr().replace("/", "") ) );        
    	eMailTV.setText( pB.geteMail() );

    	
    	//To let the textview look like a link
    	ssb.append( pB.getAddress() );
    	ssb.setSpan(new URLSpan("#"), 0, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    	
    	dienstOrtTV.setText(ssb, TextView.BufferType.SPANNABLE);    	
    	
    	
    	//dienstOrtTV.setText( pB.getAddress() );
    	
    	
    	dienstOrtTV.setOnClickListener(this);
    	
    	//dienstOrtTV.setText( Html.fromHtml("http://m.google.de/maps?q=my+Grasweg+29,+8570+Voitsberg,+Austria&hl=de") ); //pB.getAddress() );            	
    	
    	
        
        //Log.e("id: ", ApplicationDataTransfer.getInstance().getCurrentWorkingPerson().getTelephoneNr() );
        
    	
        
    }



	public void onClick(View arg0) {

		if ( arg0.getId() == dienstOrtTV.getId() ){
			
			//Starting a map utilizing an intent
			String uri = "geo:"+ "0" + "," + "0" + "?q=" + pB.getAddress().replace(" ", "+");
			startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri)));
			
			return;
			
		}
		
		
		if ( arg0.getId() == importContact.getId() ){
			
			ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
			
			int rawContactInsertIndex = ops.size();

			ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
			   .withValue(RawContacts.ACCOUNT_TYPE, null)
			   .withValue(RawContacts.ACCOUNT_NAME,null )
			   .build());
			
			ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
			   .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
			   .withValue(Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE)
			   .withValue(Phone.NUMBER, pB.getTelephoneNr())
			   .build());
			
			ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
			   .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
			   .withValue(Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE)
			   .withValue(StructuredName.DISPLAY_NAME, pB.getFullNameWithDegress())
			   .build());
			
			
			ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
					   .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
					   .withValue(Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE)
					   .withValue(CommonDataKinds.Email.DATA1, pB.geteMail())
					   .build());  			
			
			
			try {
				
				ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
				
				Toast.makeText(getBaseContext(), pB.getFullNameWithDegress() + " wurde in die Kontakliste importiert.", Toast.LENGTH_SHORT).show();
				
			} catch (Exception e) {
				
				Log.e("Exception", e.toString());
				
			}			}
			
            return;
			
		}
		
		
    
    
}